package org.grow.cooldown.ScoreModule;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.FindIterable;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginIntercepter implements HandlerInterceptor {
    public final static String cooldownSessions
            = "coolsessions";
    public final static String redisId = "JRedisId";
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    MongoTemplate mongoTemplate;
//    @Autowired
//    ObjectMapper objectMapper;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("nice to see you");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (StringUtils.isEmpty(username))
            throw new LoginExcpt(1001, "username not received from front end");
        String r = String.format("{username:'%s'}", username);
        System.out.println(r);
        Document userinfo = mongoTemplate.getCollection("userinfo").
                find(Document.parse(r)).first();
        if (userinfo == null || userinfo.isEmpty()) {
            ScoreExceptionHandler.throwEx(1002,
                    "mongodb://xxx/test1/userinfo/ miss record about " + username, response);
            return false;
        }
//            throw new LoginExcpt(1002,"mongodb://xxx/test1/userinfo/ miss record about "+username);
        if (!userinfo.get("password").equals(password))
            throw new LoginExcpt(1003, "password or username is not correct");
        String uuid = UUID.randomUUID().toString();
        String key = cooldownSessions + ":" + uuid;
        redisTemplate.opsForValue().set(key, userinfo, 10, TimeUnit.MINUTES);
        Cookie cookie = new Cookie(redisId, key);
        response.addCookie(cookie);

        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView
            modelAndView) throws Exception {
        System.out.println("0000--似乎不会运行--0000");
    }

    @Autowired
    ObjectMapper objectMapper;


    public static class LoginExcpt extends RuntimeException {
        public LoginExcpt(int code, String message) {
            super(message);
            this.code = code;
        }

        private int code;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }


    }


}
